﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Web.Administration;

namespace HostAssistantCore
{
    public class AppPoolUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="managedPipelineMode">Classic/Integrated</param>
        /// <param name="managedRuntimeVersion"></param>
        /// <returns></returns>
        public static bool Create(string name, string managedPipelineMode, string managedRuntimeVersion)
        {

            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    if (iisManager.ApplicationPools.Count(o => o.Name == name) > 0)
                    {

                        iisManager.ApplicationPools[name].AutoStart = true;
                        iisManager.ApplicationPools[name].ManagedPipelineMode = managedPipelineMode == "Classic" ? ManagedPipelineMode.Classic : ManagedPipelineMode.Integrated;//
                        iisManager.ApplicationPools[name].ManagedRuntimeVersion = managedRuntimeVersion;
                        iisManager.ApplicationPools[name].Enable32BitAppOnWin64 = true;
                        iisManager.CommitChanges(); return true;

                    };

                    ApplicationPool appPool = iisManager.ApplicationPools.Add(name);
                    appPool.AutoStart = true;
                    appPool.ManagedPipelineMode = managedPipelineMode == "Classic" ? ManagedPipelineMode.Classic : ManagedPipelineMode.Integrated;//
                    appPool.ManagedRuntimeVersion = managedRuntimeVersion;
                    appPool.Enable32BitAppOnWin64 = true;
                    //ApplicationPoolCpu cpu = appPool.Cpu;
                    //cpu.Action = ProcessorAction.KillW3wp;
                    //cpu.Limit = 90;
                    //cpu.ResetInterval = new TimeSpan(0, 1, 0);
                    //ApplicationPoolProcessModel processModel = appPool.ProcessModel;
                    //processModel.ShutdownTimeLimit = new TimeSpan(0, 0, 10);
                    //processModel.StartupTimeLimit = new TimeSpan(0, 0, 10);
                    //processModel.PingInterval = new TimeSpan(0, 0, 10);
                    //ApplicationPoolRecycling recycling = appPool.Recycling;
                    //recycling.PeriodicRestart.Time = new TimeSpan(6, 0, 0);
                    //recycling.PeriodicRestart.Schedule.Add(new TimeSpan(7, 0, 0));
                    //recycling.PeriodicRestart.Schedule.Add(new TimeSpan(13, 0, 0));
                    iisManager.CommitChanges();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        public static int CreatePoolWithID(string name, string pwd, string managedPipelineMode, string managedRuntimeVersion)
        {
            int errorCode = ErrorCode.poolBuild;
            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    if (iisManager.ApplicationPools.Count(o => o.Name == name) > 0)
                    {
                        iisManager.ApplicationPools[name].AutoStart = true;
                        iisManager.ApplicationPools[name].ManagedPipelineMode = managedPipelineMode == "Classic" ? ManagedPipelineMode.Classic : ManagedPipelineMode.Integrated;//
                        iisManager.ApplicationPools[name].ManagedRuntimeVersion = managedRuntimeVersion;
                        iisManager.ApplicationPools[name].Enable32BitAppOnWin64 = true;
                        iisManager.ApplicationPools[name].ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
                        iisManager.ApplicationPools[name].ProcessModel.UserName = System.Configuration.ConfigurationManager.AppSettings.Get("cpoolname") + name;
                        iisManager.ApplicationPools[name].ProcessModel.Password = pwd;
                        iisManager.CommitChanges(); return errorCode;

                    };
                    ApplicationPool appPool = iisManager.ApplicationPools.Add(name);
                    appPool.AutoStart = true;
                    appPool.ManagedPipelineMode = managedPipelineMode == "Classic" ? ManagedPipelineMode.Classic : ManagedPipelineMode.Integrated;//
                    appPool.ManagedRuntimeVersion = managedRuntimeVersion;
                    appPool.Enable32BitAppOnWin64 = true;
                    appPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
                    appPool.ProcessModel.UserName = System.Configuration.ConfigurationManager.AppSettings.Get("cpoolname") + name;
                    appPool.ProcessModel.Password = pwd;
                    //ApplicationPoolCpu cpu = appPool.Cpu;
                    //cpu.Action = ProcessorAction.KillW3wp;
                    //cpu.Limit = 90;
                    //cpu.ResetInterval = new TimeSpan(0, 1, 0);
                    //ApplicationPoolProcessModel processModel = appPool.ProcessModel;
                    //processModel.ShutdownTimeLimit = new TimeSpan(0, 0, 10);
                    //processModel.StartupTimeLimit = new TimeSpan(0, 0, 10);
                    //processModel.PingInterval = new TimeSpan(0, 0, 10);
                    //ApplicationPoolRecycling recycling = appPool.Recycling;
                    //recycling.PeriodicRestart.Time = new TimeSpan(6, 0, 0);
                    //recycling.PeriodicRestart.Schedule.Add(new TimeSpan(7, 0, 0));
                    //recycling.PeriodicRestart.Schedule.Add(new TimeSpan(13, 0, 0));
                    iisManager.CommitChanges();
                }
                catch
                {
                    errorCode = ErrorCode.poolNotBuild;
                }
            }
            return errorCode;
        }
        public static bool CreatePoolWithID_Q(string name, string pwd, string managedPipelineMode, string managedRuntimeVersion)
        {
            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    if (iisManager.ApplicationPools.Count(o => o.Name == name) > 0)
                    {
                        return true;
                    };
                    ApplicationPool appPool = iisManager.ApplicationPools.Add(name);
                    appPool.AutoStart = true;
                    appPool.ManagedPipelineMode = managedPipelineMode == "Classic" ? ManagedPipelineMode.Classic : ManagedPipelineMode.Integrated;//
                    //if (managedRuntimeVersion == "")
                    //{
                    //    managedRuntimeVersion = "v2.0";
                    //}
                    appPool.ManagedRuntimeVersion = managedRuntimeVersion;
                    appPool.Enable32BitAppOnWin64 = true;
                    appPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
                    appPool.ProcessModel.UserName = System.Configuration.ConfigurationManager.AppSettings.Get("cpoolname") + name;
                    appPool.ProcessModel.Password = pwd;
                    //ApplicationPoolCpu cpu = appPool.Cpu;
                    //cpu.Action = ProcessorAction.KillW3wp;
                    //cpu.Limit = 90;
                    //cpu.ResetInterval = new TimeSpan(0, 1, 0);
                    //ApplicationPoolProcessModel processModel = appPool.ProcessModel;
                    //processModel.ShutdownTimeLimit = new TimeSpan(0, 0, 10);
                    //processModel.StartupTimeLimit = new TimeSpan(0, 0, 10);
                    //processModel.PingInterval = new TimeSpan(0, 0, 10);
                    //ApplicationPoolRecycling recycling = appPool.Recycling;
                    //recycling.PeriodicRestart.Time = new TimeSpan(6, 0, 0);
                    //recycling.PeriodicRestart.Schedule.Add(new TimeSpan(7, 0, 0));
                    //recycling.PeriodicRestart.Schedule.Add(new TimeSpan(13, 0, 0));
                    iisManager.CommitChanges();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        public static bool SetPool(string name, string managedPipelineMode, string managedRuntimeVersion, string Enable32BitAppOnWin64)
        {

            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    managedPipelineMode = managedPipelineMode.ToLower();

                    iisManager.ApplicationPools[name].AutoStart = true;
                    iisManager.ApplicationPools[name].ManagedPipelineMode = managedPipelineMode == "classic" ? ManagedPipelineMode.Classic : ManagedPipelineMode.Integrated;//
                    iisManager.ApplicationPools[name].ManagedRuntimeVersion = "";
                    if (managedRuntimeVersion.Contains("2.0")) { iisManager.ApplicationPools[name].ManagedRuntimeVersion = "v2.0"; }
                    else
                    {
                        if (managedRuntimeVersion.Contains("4.0")) { iisManager.ApplicationPools[name].ManagedRuntimeVersion = "v4.0"; }
                    }

                    if (Enable32BitAppOnWin64 != "")
                    {
                        if (Enable32BitAppOnWin64 == "true")
                        {
                            iisManager.ApplicationPools[name].Enable32BitAppOnWin64 = true;
                        }
                        else { iisManager.ApplicationPools[name].Enable32BitAppOnWin64 = false; }

                    }
                    iisManager.CommitChanges(); return true;



                }
                catch
                {
                    return false;
                }
            }
        }

        public static int siteclout(string name)
        {
            using (ServerManager iisManager = new ServerManager())
            {
                return iisManager.ApplicationPools[name].WorkerProcesses.Count();
            }
        }

        public static bool Remove(string name)
        {
            using (ServerManager iisManager = new ServerManager())
            {
                ApplicationPool apool = iisManager.ApplicationPools[name];
                if (apool != null)
                {
                    iisManager.ApplicationPools.Remove(apool);
                    iisManager.CommitChanges();
                }
                return true;
            }
        }

        public static bool Recycle(string name)
        {
            using (ServerManager iisManager = new ServerManager())
            {
                iisManager.ApplicationPools[name].Recycle();
                return true;
            }
        }

        public static bool Start(string name)
        {
            using (ServerManager iisManager = new ServerManager())
            {
                iisManager.ApplicationPools[name].Start();
                return true;
            }
        }

        public static bool Stop(string name)
        {
            using (ServerManager iisManager = new ServerManager())
            {
                iisManager.ApplicationPools[name].Stop();
                return true;
            }
        }

        public static List<string> List()
        {
            using (ServerManager iisManager = new ServerManager())
            {
                return iisManager.ApplicationPools.Select(o => o.Name).ToList();
            }



        }

    }
}
